package pers.cz.selenium.component;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pers.cz.websocket.MessagePushSocketUtils;

import java.io.IOException;

/**
 * @program: PostGirl-panent
 * @description: 抽象组件：提供组件的常用功能
 * @author: Cheng Zhi
 * @create: 2022-12-24 21:08
 **/
public abstract class AbstractComponent {

    public Logger logger = LoggerFactory.getLogger(AbstractComponent.class);
    WebElement element;
    RemoteWebDriver driver;
    public void init() {
        this.element = initWebElement();
        this.driver = initDriver();
    }

    public abstract WebElement initWebElement();

    public abstract RemoteWebDriver initDriver();

    /**
     * 单击
     */
    public void click() {
        element.click();
        // 每个节点运行完成后都要推送消息到前端
        try {
            MessagePushSocketUtils.sendMsg(MessagePushSocketUtils.AUTOUI_LOG_MESSAGE, element.getText() +"单击完成", 0 );
        } catch (Exception e) {
            logger.error("获取按钮内容异常，可能是图片，请检查", e);
        }
    }

    /**
     * 双击
     */
    public void dbClick() {

        Actions sj = new Actions(driver);
        sj.doubleClick(element).build().perform();  //对按钮进行双击
        MessagePushSocketUtils.sendMsg(MessagePushSocketUtils.AUTOUI_LOG_MESSAGE, element.getText() +"双击完成", 0 );
    }

    /**
     * 右击
     */
    public void RigthClick() {
        Actions actions = new Actions(driver);
        // 触发鼠标右键点击
        actions.contextClick(element).perform();
        MessagePushSocketUtils.sendMsg(MessagePushSocketUtils.AUTOUI_LOG_MESSAGE, element.getText() + "右击完成", 0 );
    }

    /**
     * 输入
     * @param value
     */
    public void setValue(String value) {
        element.sendKeys(value);
        MessagePushSocketUtils.sendMsg(MessagePushSocketUtils.AUTOUI_LOG_MESSAGE, "输入完成，内容为" + value, 0 );
    }

}
